//
//  AppStoreViewController.m
//  reSearchDemo
//
//  Created by 云库 on 15/9/16.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "AppStoreViewController.h"
#import "UIView+Category.h"
#import "AFNetworking.h"
#import "BusinessClass.h"
#import "BusinessClassInfo.h"
#import "MJExtension.h"
#import "MBProgressHUD.h"
#import "BusinessNearInfo.h"//附近商品类 cell
#import "BusinessNear.h"
#import "InterestTableViewCell.h"
#import "GoodsSortVC.h"
#import "MoreSortVC.h"



@interface AppStoreViewController()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIView *headerView;

/** 返回的分类列表模型*/
@property (strong, nonatomic) NSArray *classesArr;
/** 附近类数据模型*/
@property (strong, nonatomic) NSMutableArray *nearGoodsArr;

@end

@implementation AppStoreViewController

/** 初始化*/
- (NSArray *)classesArr {
    if (!_classesArr) {
        _classesArr = [NSArray array];
    }
    return _classesArr;
}

- (NSMutableArray *)nearGoodsArr{
    if (!_nearGoodsArr) {
        _nearGoodsArr = [NSMutableArray array];
    }
    return _nearGoodsArr;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    //加在tableView并设置头部视图
    [self loadTableView];
    //加在cell所需数据
    [self getInterestGoodsData];
    
    //添加监听 当用户点击自动以headerView礼里面的分类按钮的时候响应
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(clickedCategory:) name:@"clickedCategory" object:nil];
}

/**
 * 首页展示的cell数据
 */
- (void)getInterestGoodsData{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
#warning 暂时用的是附近类数据
    NSString *path = [NSString stringWithFormat:@"%@%@",SHOPER,@"html/ashop/nearshop.do?lon=104&lat=31&dist=100&pageno=1&type=2"];
    [manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        BusinessNearInfo *info = [BusinessNearInfo objectWithKeyValues:responseObject];
        NSArray *nearGoodsArr = info.data;
        [self.nearGoodsArr addObjectsFromArray:nearGoodsArr];

        [self.tableView reloadData];
            }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
#warning TODO
        DLog(@"请求网络失败");
    }];
    
}

/**
 * 发请求获得头部分类信息
 */
- (void)loadTableView{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://mall.yunku.mobi:8080/mall/html/amerchant/listSJType.do?pageno=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        BusinessClassInfo *info = [BusinessClassInfo objectWithKeyValues:responseObject];
        self.classesArr = info.data;
        //设置
        [self setUpTableView];
        [self setUpHeaderView];
        
        [self.tableView reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
#warning TODO
        DLog(@"请求网络失败");
    }];
    
    
}

/**
 * 点击分类按钮 传递分类模型
 */
-(void)clickedCategory:(NSNotification*)notif{
    
    BusinessClass *category = [notif.userInfo objectForKey:@"category"];
    DLog(@"%@",category.name);
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString * str = @"猜你喜欢";
    
    return str;
}


/**
 * 设置表头视图分类按钮
 */
- (void)setUpHeaderView{
  
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 290)];
    self.tableView.tableHeaderView = self.headerView;
    
    /** 分类按钮*/
    UIView *classViewBtn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 170)];
    classViewBtn.backgroundColor = [UIColor lightGrayColor];
    [self.headerView addSubview:classViewBtn];
    
    for (int i = 0; i<8; i++) {
        //i%4*(self.view.width/4)+12, i/4*90+10, 60, 60)
        UIButton * headerBtn = [[UIButton alloc]initWithFrame:CGRectMake(i%4*(self.view.width/4)+15, i/4*80+5, 55, 55)];
        headerBtn.tag = i;
        [headerBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"分类%d.png",i]] forState:UIControlStateNormal];
        [headerBtn addTarget:self action:@selector(categoryClick:) forControlEvents:UIControlEventTouchUpInside];
        [classViewBtn addSubview:headerBtn];
        
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(i%4*(self.view.width/4)+15, i/4*80+65, 55, 10)];
        BusinessClass *city = self.classesArr[i];
        label.text = city.name;
        if (i == 7) {
            label.text = @"更多";
        }
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont fontWithName:@"黑体-简" size:9];
        label.textColor = [UIColor colorWithRed:62.0/255 green:62.0/255 blue:62.0/255 alpha:1];
        [classViewBtn addSubview:label];
    }
    
    /** 广告按钮*/
    UIButton *adViewBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, classViewBtn.height, self.view.width, self.headerView.height-classViewBtn.height)];
    [adViewBtn setBackgroundImage:[UIImage imageNamed:@"云库广告图片.jpg"] forState:UIControlStateNormal];
    [adViewBtn addTarget:self action:@selector(adViewBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:adViewBtn];

    
}

/**
 * 广告按钮店家方法
 */
- (void)adViewBtnClick{
    DLog(@"点击广告图片");
}

/**
 * TableView
 */
- (void)setUpTableView{
    
    //2.创建table view
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    [self.view addSubview:self.tableView];
}

/**
 * 点击按钮发送通知--传递分类模型
 */
-(void)categoryClick:(UIButton*)btn{
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"clickedCategory" object:nil userInfo:@{@"category":self.classesArr[btn.tag]}];
   
    GoodsSortVC *goodsSortVC = [[GoodsSortVC alloc] init];
    [self pushViewController:goodsSortVC];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshDataListIfNeed{
    
    
}

#pragma mark - **************** TableViewDelegate的方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.nearGoodsArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *cellId = @"InterestTableViewCell";
    InterestTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
            cell = [[[NSBundle mainBundle]loadNibNamed:@"InterestTableViewCell" owner:self options:nil]lastObject];
    }
    BusinessNear * business = self.nearGoodsArr[indexPath.row];
    cell.business = business;
    
    
        return cell;
    }


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DLog(@"点击了cell");
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}


@end
